/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services.system;

/**
 *
 * @author jay
 */

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.faces.model.SelectItem;
import src.domain.system.Branch;
import src.services.CRUEDService;
import src.util.Utils;

public class BranchModel implements java.io.Serializable {
    
    private CRUEDService service;
    
    public BranchModel() {
        service = new CRUEDService();
    }
    
    public List<Branch> findAll(Map<String, String> filters) {
        return (List<Branch>) service.findAll("Branch", filters);
    }
    
    public List<SelectItem> listAll() {
        return service.listAll("Branch", "branchId","branchName");
    }

    public Integer getBranchPc(String ip) {
        BigDecimal hasil;
        try { hasil = (BigDecimal) service.getObjectSQLdirect("select branch_id from branch_pc where ip_address = "+Utils.fmtSQLString(ip));
        } catch (RuntimeException e) {hasil = BigDecimal.ZERO;}
        
        if (hasil!=null) 
        return hasil.intValue();
        else return null;
    }
    
    public boolean isBranchExist(String value, Integer id) {
        BigDecimal count;
        
        String cond = " where branch_no = "+Utils.fmtSQLString(value);
        if (id != null) cond+=" and branch_id <> "+id;
        
        try { count = (BigDecimal) service.getObjectSQLdirect("select count(*) from branch "+cond);
        } catch (RuntimeException e) {count = BigDecimal.ZERO;}
        
        if (count==null) count = BigDecimal.ZERO;
        return (count.compareTo(BigDecimal.ZERO)==0 ? false : true);
    }
    
    public boolean isIPExist(String ip, Integer branchId) {
        BigDecimal count;
        String cond = " where ip_address = "+Utils.fmtSQLString(ip);
        if (branchId != null) cond+=" and branch_id <> "+branchId;
        
        try { count = (BigDecimal) service.getObjectSQLdirect("select count(*) from branch_pc "+cond);
        } catch (RuntimeException e) {count = BigDecimal.ZERO;}
        
        if (count==null) count = BigDecimal.ZERO;
        return (count.compareTo(BigDecimal.ZERO)==0 ? false : true);
    }
    
    public boolean saveData(Branch data) {
        return service.saveData(data);
    }
    
    public boolean deleteData(Branch data) {
        return service.deleteData(data);
    }
}
